// JavaScript Document
/*
 *  class：common.js  
 *  version：1.0
 *  author：梁丘尘鸣
 */
(function($) {
    $.fn.extend({
        //拖拽函数
        /*可选参数：
         *  @json defaultVal={
         *  @string    'not': ".nodr", //不允许拖拽的标签名;默认：true
         *  @string    'limit':".Dr",  //限制父级窗口;默认window
         *  @int       'zindex': 999  //层级z-index;默认true
         *                   }
         * 使用方法：
         *          $(".zdr").Drag(Drb); $(".zdr").Drag()
         */
        "Drag": function(options) {
            //定义默认值
            var defaultVal = {
                'not': true,
                'limit': window,
                'zindex': true
            };
            //赋值默认值
            var obj = $.extend(defaultVal, options);
            //可以链式操作
            return this.each(function() {
                var move = false;
                var x, y, nowLeft, nowTop;
                var marginY, marginX;
                var _this = $(this);
                //设置不想被拖拽的
                if (obj.not !== true) {

                    $(obj.not).mousedown(function(e) {
                        move = false;
                        e.stopPropagation();
                    }).css({
                        cursor: 'default'
                    });
                }
                _this.mousedown(function(e) {

                    move = true;
                    //设置索引
                    if (obj.not !== true) {
                        _this.css({
                            'z-index': obj.zindex
                        });
                    }
                    x = e.pageX;
                    y = e.pageY;
                    marginY = (_this.outerHeight(true) - _this.outerHeight()) / 2;
                    marginX = (_this.outerWidth(true) - _this.outerWidth()) / 2;
                    nowLeft = _this.position().left;
                    //当前位置
                    nowTop = _this.position().top;
                    $(document).bind("mousemove", dragMove);
                    return false;
                }).mouseup(function() {
                    move = false;
                }).mouseenter(function() {
                    _this.css({
                        cursor: 'move'
                    });
                });
                var dragMove = function(e) {
                    if (!move)
                        return false;
                    //最终位移=移动位置+当前位置
                    var limitX = e.pageX - x + nowLeft;
                    var limitY = e.pageY - y + nowTop;
                    var maxX = $(obj.limit).innerWidth() - _this.outerWidth() - marginX;
                    var maxY = $(obj.limit).innerHeight() - _this.outerHeight() - marginY;
                    limitX = (limitX < -marginX) ? -marginX : limitX;
                    limitX = (limitX < maxX) ? limitX : maxX;
                    limitY = (limitY < -marginY) ? -marginY : limitY;
                    limitY = (limitY < maxY) ? limitY : maxY;
                    //限制窗口
                    _this.css({
                        "position": 'absolute',
                        'left': limitX,
                        "top": limitY
                    });
                };

            });
        },
        //让标签相对于window居中
        /* 可选参数：无
         * 使用方法： $(".cen").cencer();
         */
        "cencer": function() {
            return this.each(function() {
                var _this = $(this);
                var winY = $(window).height();
                var winX = $(window).width();
                var popX = _this.outerWidth(true);
                var popY = _this.outerHeight(true);
                var cenX = (winX - popX) / 2;
                var cenY = 200 + $(document).scrollTop();
                _this.css({
                    "position": 'absolute',
                    'left': cenX,
                    'top': cenY
                });
            });
        },
        //DIV固定不动
        'Fixed': function() {
            return this.each(function() {
                var _this = $(this);
                var topX = _this.offset().top;
                //求出外边距
                var fixfn = function() {
                    _this.css({
                        'margin-top': '0px'
                    });
                    var scrollY = $(window).scrollTop();
                    if (scrollY > topX)
                        _this.css({
                            'position': 'fixed',
                            'top': '0px',
                            'left': '0px'
                        });
                    else
                        _this.removeAttr("style");
                };
                $(window).bind('scroll', fixfn);
            });
        },
        //悬停消息提示
        /*可选参数：
         * @String content:提示标签，默认：无，
         * @fn enterfn:鼠标移入提示时，执行的函数  默认：可以为空
         * @fn leavefn：鼠标移除提示时，执行的函数 默认：可以为空
         * 使用方式：
         *  $(".tt").HoverTip(".htips", function(e) {},  function() {});
         *  $(".tt").HoverTip(".htips");
         */
        'HoverTip': function(content, enterfn, leavefn) {
            return this.each(function() {
                var _this = $(this);

                _this.mouseenter(function(e) {
                    if (!$(".w_arn").is("div")) {
                        $("body").append('<div class="w_arn"><div>');
                    }
                    if (!$(".w_rap").is("div")) {
                        $(content).wrap("<div class='w_rap'></div>").css("margin", 0);
                    }
                    _this.css("cursor", "pointer");

                    $(".w_arn").css({
                        'z-index': 99999,
                        'position': 'absolute',
                        'top': e.pageY + 10,
                        'left': e.pageX + 10
                    });
                    var con = $(".w_rap").html();
                    $(".w_arn").html(con);
                    $(".w_arn").find(content).css({
                        'display': 'block'
                    });
                    if (enterfn === undefined) {

                    } else {
                        enterfn(_this);
                    }

                   // return false;
                }).mousemove(function(e) {
                    $(".w_arn").css({
                        'top': e.pageY + 10,
                        'left': e.pageX + 10
                    });
                  //  return false;
                }).mouseleave(function() {
                    $(".w_arn").remove();
                    $(content).unwrap();
                    if (leavefn === undefined) {
                    } else {
                        leavefn(_this);
                    }

                   // return false;
                });
            });
        }
    });
    $.extend({
        //一般调试
        /*
         * $.Debug('这是在调试')
         */
        'Debug': function(arg) {
            if (!$("#Debug").is("span")) {
                $("body").append('<span id="Debug" style="position:fixed;border:1px solid #ccc;background:#f2f2f2;color:red;z-index:999;padding:20px;font-size:15px;font-weight:bold;opacity:0.7;top:20px;left:20%"></span>');
            }
            $("#Debug").html('<div style="color:#000 font-size:15px;font-weight:bold;">程序调试如下：&nbsp;&nbsp;<div>' + arg);
        },
        /**
         * [Ajax调试函数]
         * @param  {Function} fn [description]
         * @return {[type]}      [description]
         */
        'aDebug': function(options) {
            var strHtml = '<div id="ajax-_frame">'
                    + '<div id="ajaxDebug">点击调试</div>' 
                    + '<div id="ajax-_close">关闭调试</div>'
                     + '<div id="ajax-_clear">清除文本</div>'
                    + '<div id="ajax-_min">最小化</div>'
                    + '<div id="show-_Debug" >请点击“点击调试”按钮进行调试</div>'
                    + '</div>';
            $("body").append(strHtml);
            var _this= $("#show-_Debug");
             var defaultVal = {
                type: 'post', //类型
                url: 'index.php', //PHP脚本
                data: '',  //数据
                //msg：json数据 _this:调试窗口对象
                success: function(msg, _this) {
                   _this.html('调试结果如下：<br/>' + msg).show();
                }
            };
            //赋值默认值
            var obj = $.extend(defaultVal, options);
            $("#ajax-_frame").css({
                'z-index': 99999,
                'background': '#fff',
                'position': 'fixed',
                'top': 0,
                'left': 0,
                'padding': '10px'
            });
            $("#ajax-_close,#ajax-_min,#ajaxDebug,#ajax-_clear").css({
                'float': 'left',
                'margin-left': '10px',
                'background': '#53364c',
                'color': '#fff',
                'padding': '5px',
                'font-size': '12px',
                'font-weight': 'bold',
                'cursor': 'pointer'

            });
            $("#show-_Debug").css({
                'clear': 'both',
                'display': 'block',
                'padding': '10px',
                'background': '#fff',
                'color': '#000000',
                'word-break': 'break-all',
                'font-size': '18px'
            });
           
              $("#ajax-_clear").click(function(event) {
                $("#show-_Debug").html('');
            });
            $("#ajax-_close").click(function(event) {
                $("#ajax-_frame").remove();
            });
            $("#ajax-_min").click(function(event) {
                if ($("#show-_Debug").css('display') === 'block') {
                    $("#show-_Debug").hide();
                    $(this).text('最大化');
                } else {
                    $("#show-_Debug").show();
                    $(this).text('最小化');
                }
            });
            $("#ajaxDebug").click(function(event) {
                $.ajax({
                    type: obj.type,
                    url: obj.url,
                    data: obj.data,
                    success: function(msg) {
                        obj.success(msg, _this);

                    }
                });
            });
        },
        /*
         * $.Dtrim(" 你是谁 ");//删除两边空格
         * $.Ltrim(" 你是谁 ");//删除左边空格
         * $.Rtrim(" 你是谁 ");//删除右边空格
         */
        //删除两边空格
        'Dtrim': function(str) {
            return str.replace(/(^\s*)|(\s*$)/g, "");
        },
        //删除左边的空格
        'Ltrim': function(str) {
            return str.replace(/(^\s*)/g, "");
        },
        //删除右边的空格
        'Rtrim': function(str) {
            return str.replace(/(\s*$)/g, "");
        },
        //全屏遮罩
        /*   可选参数：
         * @int lock: 开关遮罩：默认：0：关闭遮罩 ；为空或者任意整数：值开启遮罩
         * @int zindex:设置z-index： 默认：999；可以为空
         * 使用方式：
         * $.Mask(0,22) 关闭 $.Mask(3,66)开启  $.Mask()开启
         */
        'Mask': function(lock, zindex) {
            //定义默认值
            if (zindex == null || zindex == "")
                zindex = 999;
            if (lock == 0) {
                $("#DivLocker").remove();
            } else {
                var X, Y;
                Y = $(document).height();
                X = $(document).width();
                $("body").append("<div id='DivLocker'></div>");
                $('#DivLocker').css({
                    "position": "absolute",
                    "top": "0px",
                    "left": "0px",
                    "background-color": "#000000",
                    "height": Y,
                    "filter": "alpha(opacity=30)",
                    "opacity": "0.3",
                    "overflow": "hidden",
                    "width": X,
                    "z-index": zindex
                });
                var Maskfn = function() {
                    Y = $(document).height();
                    X = $(document).width();
                    $('#DivLocker').css({
                        "height": Y,
                        "width": X
                    });
                };
                $(window).bind('resize', Maskfn);
            }
        },
        //设置弹出框
        /*
         * 可选参数
         * @String message:要提示的消息 默认：必填
         * @Json style:用json的方式 默认：默认样式  可以为空
         * @Int time:停留的时间 默认:400(毫秒) 可以为空 
         * 使用方式：
         * $.Popup('弹出成功',{'border': '2px solid #CCC','width':'100px'},1500);$Popup('弹出成功');
         */
        'Popup': function(message, style, time) {
            if (time == null)
                var time = 1500;
            if (style == "" || style == null) {
                var style = {
                    'box-shadow': '1px 0px 5px #ccc',
                    'border': '1px solid #f2f2f2',
                    'border-radius': '5px',
                    'padding': '15px 30px',
                    'font-size': '14px',
                    'background': '#F9F9F9',
                    'color': '#575757',
                    'font-weight': 'bold',
                    'font-family': '微软雅黑'
                };
            }
            if ($(".popup[tag='popup']").attr('tag'))
                return false;
            else {
                $('body').show('slow').append("<span class='popup' tag='popup'>" + message + "</span>");
                //设置弹出框的样式
                $('.popup').css({
                    'position': 'absolute',
                    'z-index': 1111
                });
                $('.popup').css(style);
                //设置居中显示
                var winX = $(window).width();
                var popX = $(".popup[tag='popup']").width();
                var cenX = (winX - popX) / 2;
                var scrol = $(document).scrollTop();
                $(".popup[tag='popup']").css({
                    'left': cenX,
                    'top': 260 + scrol
                });
                //设置动画
                setTimeout(function() {
                    $(".popup[tag='popup']").stop().animate({
                        top: 260 + scrol + 10
                    }, 200);
                    $(".popup[tag='popup']").queue(function() {
                        $(this).stop().animate({
                            opacity: 0,
                            top: 260 + scrol - 60
                        }, 300);
                        $(this).dequeue();
                    });
                }, time);
                setTimeout(function() {
                    $(".popup[tag='popup']").remove();
                }, time + 500);
            }
        }

    });
    //幻灯片插件
    /*
     *        Html:
     <div class="A">
     <div class="frame">
     <ul class="slideframe">
     <li class="slideImg">
     <img src="photo/a.jpg"></li>
     <li class="slideImg">
     <img src="photo/b.jpg"></li>
     <li class="slideImg">
     <img src="photo/c.jpg"></li>
     </ul>
     <div class="pre"></div>
     <div class="next"></div>
     <ul class="num">
     <li>1</li>
     <li>2</li>
     <li>3</li>
     </ul>
     </div>
     </div>
     */
    $.extend({
        "slide": function(option, config) {
            slideUI(option, config);
            var liwidth = parseInt($(option + " .slideImg").width());
            var i = 0;
            var time; //定时器
            var index;
            T();
            $(option + " .num li").eq(0).css({
                "opacity": 0.5
            });
            //上一页
            $(option + " .pre").click(function() {
                if (i == 0) {
                    i = 3;
                }
                i--;
                var r = i * liwidth;
                $(option + " .num li").eq(i).css({
                    "opacity": 0.5
                }).siblings().css({
                    "opacity": 1
                });
                $(option + " .slideframe").stop().animate({
                    "left": -r
                }, 300);
            })
            //下一页
            $(option + " .next").bind('click', function() {
                if (i == 2) {
                    i = -1;
                }
                i++;
                var r = i * liwidth;
                $(option + " .num li").eq(i).css({
                    "opacity": 0.5
                }).siblings().css({
                    "opacity": 1
                });
                $(option + " .slideframe").stop().animate({
                    "left": -r
                }, 300);

            })
            //鼠标走过
            $(option + " .num li").mouseenter(function() {
                index = $(this).index();
                var r = index * liwidth;
                i = index;
                $(option + " .slideframe").stop().animate({
                    "left": -r
                }, 300);
                $(this).css({
                    "opacity": 0.5
                }).siblings().css({
                    "opacity": 1
                });
                clearInterval(time);
            }).mouseleave(function() {
                T();
            })
            $(".pre,.next").mousedown(function() {

                return false;
            })
            //翻页效果
            $(option + " .next," + option + " .pre").mouseenter(function() {
                clearInterval(time);
            }).mouseleave(function() {
                T();
            });
            $(window).resize(function() {

                $(option + " .frame").css({
                    "width": $(window).width()
                });
                liwidth = $(window).width();
                $(option + " .slideframe").css({
                    "width": $(window).width() * 3
                });
                $(option + " .slideframe").stop().animate({
                    "left": 0
                }, 300);
                $(option + " .slideImg").css({
                    "width": $(window).width()
                });
                i = 0;
                $(option + " .num li").eq(0).css({
                    "opacity": 0.5
                }).siblings().css({
                    "opacity": 1
                });
            })
            //自动翻页  多次用到
            function T() {
                time = setInterval(function() {
                    if (i == 2) {
                        i = -1;
                    }
                    i++;
                    var r = i * liwidth;
                    $(option + " .num li").eq(i).css({
                        "opacity": 0.5
                    }).siblings().css({
                        "opacity": 1
                    });
                    $(option + " .slideframe").stop().animate({
                        "left": -r
                    }, 300);
                }, 3000);
            }
        }
    })
    //布局
    var slideUI = function(option, config) {
        if (config == undefined) {
            config = {
                'width': $(window).width(),
                'height': 400
            };
        }
        $(option + " .frame").css({
            "width": config.width,
            'height': config.height
        });
        var width = parseInt($(".frame").width());
        $(option + " .slideframe").css({
            "width": width * 3
        });
        $(option + " .slideframe li").css({
            "width": width
        });
        $(option + " .next," + option + " .pre").css({
            "top": (config.height - $(option + " .pre").height()) / 2
        });
    };
    $.fn.extend({
        //函数一
        "Waterfall": function(options) {
            //定义默认值
            var defaultVal = {
                'auto': false, //自适应窗口
                'childTag': ".water", //子级标签
                'betweenHeight': 10, //上下间距
                'betweenWidth': 10, //左右间距
                'num': 9, //每次加载个数
                'animateTime': 1000, //加载时间（毫秒）
                "animateStyle": 2, //动画效果
                'animateChange': false, //改变动画
                ajaxtype: "GET",
                ajaxurl: "index.php",
                ajaxdata: {
                    name: 9
                },
                successfn: function(e) { //e:this对象

                },
                //msg成功返回的数据 _this:this对象 str:最后的组装数据
                beforeSendfn: function(msg, _this, str) {

                },
                completefn: function(e) { //e:this对象

                }

            };
            //赋值默认值
            var obj = $.extend(defaultVal, options);
            //可以链式操作
            return this.each(function() {

                var _this = $(this);
                var size;
                obj.ajaxdata.num = obj.num;
                //第一次ajax加载;
                $.ajax({
                    type: obj.ajaxtype,
                    url: obj.ajaxurl,
                    data: obj.ajaxdata,
                    beforeSend: function() {
                        obj.beforeSendfn(_this);

                    },
                    success: function(msg) {
                        var str = obj.successfn(msg, _this, String);
                        _this.append(str);
                        //初始化，第一次瀑布流排序
                        size = _init(_this, obj.childTag, obj.betweenHeight, obj.betweenWidth, obj.num, obj.animateTime, obj.animateStyle);
                    },
                    complete: function() {
                        obj.completefn(_this);

                    }
                });


                //滚动条滚动后续加载
                var k = 1; //从第k*bj.num标签
                var checkAjax = true; //限制ajax加载
                /**
                 * [scrollEvent 滚动条事件处理函数]
                 * @return {[type]} [description]
                 */
                var scrollEvent = function() {
                    if (_checkScroll(size) && checkAjax) {
                        checkAjax = false;
                        $.ajax({
                            type: obj.ajaxtype,
                            url: obj.ajaxurl,
                            data: obj.ajaxdata,
                            beforeSend: function() {
                                obj.beforeSendfn(_this);
                            },
                            success: function(msg) {
                                var str = obj.successfn(msg, _this);
                                _this.append(str);
                                //再次瀑布流排序
                                _againRank(size, obj.childTag, obj.betweenHeight, k * obj.num, obj.num, obj.animateTime, obj.animateStyle);
                                k++;
                            },
                            complete: function() {
                                obj.completefn(_this);
                                checkAjax = true;
                            }
                        });
                    }
                };
                if (obj.animateChange) {

                    _this.find(obj.childTag).remove();

                    $(window).unbind('scroll').bind('scroll', scrollEvent);

                } else {
                    $(window).bind('scroll', scrollEvent);
                }

                if (obj.auto) {
                    _this.css({
                        width: '100%'
                    });
                    var rank = 1;
                    $(window).bind('resize', function(event) {
                        clearTimeout(rank);
                        rank = setTimeout(function() {
                            var getlastindex = parseInt(_this.find(obj.childTag).length);
                            size = _init(_this, obj.childTag, obj.betweenHeight, obj.betweenWidth, getlastindex, obj.animateTime, obj.animateStyle);
                        }, 500);
                    });
                }
            });
        }
    });
    //maxSize最大的高度（限制滚动条）
    var _checkScroll = function(size) {
        if (size === undefined)
            return false;
        var max = Math.max.apply(Math, size[0]);
        var windowH = $(window).height();
        var scrollH = $(window).scrollTop();
        if (max <= (windowH + scrollH) + 100) {
            return true;
        }
        return false;
    };
    //初始化第一次排列
    //betweenH 上下间距
    //betweenW 左右间距
    //animateTime 运动时间 
    //animateStyle 运动风格
    //return size[0] 宽度数组  size[1] 高度数组
    var _init = function(parentTag, childTag, betweenHeight, betweenWidth, num, animateTime, animateStyle) {
        var HA = [];
        var WA = [];
        var size = [];
        var tag = $(childTag);

        //计算第一行有多少个
        var fastNum = Math.floor($(parentTag).outerWidth() / tag.outerWidth());
        var oneWidthBetween = Math.floor(($(parentTag).outerWidth() - (tag.outerWidth() * fastNum) - betweenWidth * (fastNum - 1)) / 2);
        //获取每一个的高度
        var W = oneWidthBetween;

        for (var i = 0; i < fastNum; i++) {
            if (i != 0 && i != fastNum) {
                W = W + betweenWidth;
            }
            WA[i] = W;
            // tag.eq(i).css({'left': W, 'top': betweenHeight});
            setAnimate(childTag, i, W, betweenHeight, animateTime, animateStyle); //设置动画
            //获取每一个的宽度
            W = W + tag.outerWidth();
            var H = tag.eq(i).outerHeight() + betweenHeight;
            HA[i] = H;
            //排列
        }

        size[1] = WA;
        size[0] = HA;
        //  alert(num);
        //排列完剩下的
        _againRank(size, childTag, betweenHeight, fastNum, num - fastNum, animateTime, animateStyle);
        //alert(size[0]);
        return size;
    };
    //再次一个一个排列
    //i:第I个元素
    var _againRank = function(size, childTag, betweenHeight, lastnum, num, animateTime, animateStyle) {
        // alert(lastnum+'---'+(num));
        for (var i = lastnum; i < lastnum + num; i++) {

            var sort = _minArr(size[0]);
            var min = sort[0];
            var index = sort[1];

            var H = $(childTag).eq(i).outerHeight();
            size[0][index] = size[0][index] + H + betweenHeight;
            // $(childTag).eq(i).css({'left': size[1][index], 'top': (min + betweenHeight)});
            setAnimate(childTag, i, size[1][index], (min + betweenHeight), animateTime, animateStyle);
        }
    };
    //设置动画
    /*
     * @param {type} selector 选择器
     * @param {type} index 限制运动索引
     * @param {type} left left值
     * @param {type} top top值
     * @param {type} style 设置运动风格
     */
    //限制动画的运动
    var setAnimate = function(childTag, i, left, top, time, style) {
        var cencer = ($(window).width()) / 2;
        var H = $(document).height();
        //运动时间要设置
        $(childTag).eq(i).queue('watermove', function() {

            switch (style) {
                case 1:
                    {
                        $(this).css({
                            opacity: 0,
                            right: 0,
                            "top": top
                        });
                    }
                    break;
                case 2:
                    {
                        $(this).css({
                            left: left,
                            top: H,
                            opacity: 0
                        });
                    }
                    break;
                case 3:
                    {
                        $(this).css({
                            left: cencer,
                            "top": top,
                            opacity: 0
                        });
                    }
                    break;
                case 4: //淡入淡出
                    {
                        $(this).css({
                            left: left,
                            "top": top,
                            opacity: 0
                        });
                    }
                    break;
                case 5:
                    {
                        $(this).css({
                            left: left,
                            "top": top
                        });
                        $(this).animate({
                            left: left - 100
                        }, 500);
                    }
                    break;
            }
            $(this).animate({
                left: left,
                'top': top,
                opacity: 1
            }, time);
        })
        $(childTag).eq(i).dequeue('watermove');
    };
    //计算数组中的最小值
    var _minArr = function(arr) {
        var index = 0,
                min = arr[0];
        var sort = new Array();
        for (var i = 0; i < arr.length; i++) {
            if (min > arr[i] && i != 0) {
                min = arr[i];
                index = i;
            }
        }
        sort[0] = min;
        sort[1] = index;
        return sort;
    };

    $.fn.extend({
        'scrollBar': function(options) {
            var defaults = {
                listW: 200,
                //列表宽
                listH: 300,
                //列表高
                speed: 10,
                //滚动速度
                autoHide: 0,
                //设置滚动条是否自动隐藏 1:自动隐藏 0：不自动隐藏， 默认不隐藏
                CSS: 1
            };
            var config = $.extend(defaults, options);
            return this.each(function() {
                var nowthis = $(this);
                var documentEvent, documentEvent2;
                if (nowthis.outerHeight(true) < config.listH) {
                    nowthis.siblings("#scroll_bar").remove();
                    nowthis.unwrap().removeAttr("style");
                } else {
                    _wrapHtml(nowthis, config.listH, config.listW);
                    if (config.CSS !== 1) {
                        nowthis.siblings("#scroll_bar").css(config.CSS);
                    } else {
                        nowthis.siblings("#scroll_bar").css({
                            margin: 0,
                            padding: 0,
                            background: "#f9f9f9",
                            border: "1px solid #ccc",
                            "border-radius": "5px"
                        });
                    }
                    var overH = _getOverHeght(nowthis, config.listH);
                    _removeEvent(nowthis, documentEvent, documentEvent2);
                    _dragBar(nowthis, overH);
                    _mouseWheelBar(nowthis, overH, config.listH, config.speed);
                    if (config.autoHide) {
                        _isBarHide(nowthis);
                    }
                }
            });
        }
    });
    /**
     * [_wrapHtml description]
     * @param  {[obj]} nowthis [当前标签]
     * @param  {[int]} listH   [列表高]
     * @param  {[int]} listW   [列表宽]
     */
    function _wrapHtml(nowthis, listH, listW) {
        if (nowthis.parent("#scroll_frame").size())
            return false;
        nowthis.wrap('<div id="scroll_frame"></div>');
        nowthis.parent("#scroll_frame").prepend('<div id="scroll_bar"><div>');
        nowthis.parent("#scroll_frame").css({
            overflow: "hidden",
            height: listH,
            width: listW,
            position: "relative"
        });
        nowthis.siblings("#scroll_bar").css({
            position: "absolute",
            right: 0,
            top: 0,
            "z-index": 2,
            width: "7px",
            background: "#f9f9f9"
        });
        nowthis.css({
            position: "absolute",
            "z-index": 1
        }).outerWidth(listW);
    }
    /**
     * [_getOverHeght 设置可移动高度]
     * @param  {[obj]} nowthis [当前标签]
     * @param  {[int]} listH   [列表高]
     * @return {[int]}         [可移动的高度]
     */
    function _getOverHeght(nowthis, listH) {
        //超过的
        var overH = nowthis.outerHeight(true) - listH;
        var DragH = 0;
        //设置滚动条高度
        if (overH < listH - 20) {
            nowthis.siblings("#scroll_bar").outerHeight(listH - overH);
            DragH = 1;
        } else {
            nowthis.siblings("#scroll_bar").outerHeight(listH / 5);
            //可移动高度
            DragH = overH / (listH - listH / 5);
        }
        var buttomH = listH - nowthis.siblings("#scroll_bar").position().top - nowthis.siblings("#scroll_bar").outerHeight(true);
        var isbottom = nowthis.outerHeight(true) + nowthis.position().top;
        if (isbottom < listH) {
            nowthis.css("top", -overH);
        }
        if (buttomH < 0) {
            nowthis.siblings("#scroll_bar").css("top", overH);
        }
        return DragH;
    }
    /**
     * [_dragBar 滚动条拖拽函数]
     * @param  {[obj]} nowthis        [当前的标签]
     * @param  {[int]} overH          [超过列表的高度]
     * @param  {[function]} documentEvent  [document事件1]
     * @param  {[function]} documentEvent2 [document事件2]
     */
    function _dragBar(nowthis, overH, documentEvent, documentEvent2) {
        var x, y, nowLeft, nowTop, marginY, marginX;
        var move = false;
        var _this = nowthis.siblings("#scroll_bar");
        _this.bind("mousedown", function(e) {
            move = true;
            _this.css({
                opacity: 1
            });
            y = e.pageY;
            marginY = (_this.outerHeight(true) - _this.outerHeight()) / 2;
            //当前位置
            nowTop = _this.position().top;
            $(document).bind("mousemove", documentEvent).bind("mouseup", documentEvent2);
            return false;
        }).bind("mouseup", function() {
            move = false;
        }).bind("mouseenter", function() {
            _this.css({
                cursor: "move"
            });
        });
        documentEvent = function(e) {
            if (!move)
                return false;
            //最终位移=移动位置+当前位置
            var limitY = e.pageY - y + nowTop;
            var maxY = nowthis.parent("#scroll_frame").innerHeight() - _this.outerHeight() - marginY;
            limitY = limitY < -marginY ? -marginY : limitY;
            limitY = limitY < maxY ? limitY : maxY;
            //限制窗口
            _this.css({
                opacity: 1,
                top: limitY
            });
            nowthis.css({
                top: -overH * limitY
            });
            return false;
        };
        documentEvent2 = function(event) {
            move = false;
        };

    }
    /**
     * [_mouseWheelBar 滚轮事件]
     * @param  {[obj]} nowthis        [当前的标签]
     * @param  {[int]} overH          [超过列表的高度]
     * @param  {[int]} listH    [列表高]
     * @param  {[int]} speed   [滚动速度]
     * @return {[bool]}         [false]
     */
    function _mouseWheelBar(nowthis, overH, listH, speed) {
        nowthis.parent("#scroll_frame").bind("mousewheel", function(event, delta) {
            var Y = parseInt(nowthis.siblings("#scroll_bar").position().top);
            var H = listH - nowthis.siblings("#scroll_bar").outerHeight(true);
            Y = Y + speed * -delta;
            //滚动条范围
            Y = Y <= 0 ? 0 : Y;
            Y = Y >= H ? H : Y;
            nowthis.siblings("#scroll_bar").css({
                opacity: 1,
                top: Y
            });
            nowthis.css({
                top: -overH * Y
            });
            return false;
        });
    }
    /**
     * [_removeEvent 移除事件]
     * @param  {[function]} documentEvent  [document事件1]
     * @param  {[function]} documentEvent2 [document事件2]
     */
    function _removeEvent(nowthis, documentEvent, documentEvent2) {
        nowthis.siblings("#scroll_bar").unbind();
        nowthis.parent("#scroll_frame").unbind();
        $(document).unbind("mousemove", documentEvent);
        $(document).unbind("mouseup", documentEvent2);
    }
    //
    /**
     * [_isBarHide 设置滚动条是否自动隐藏]
     */
    function _isBarHide(nowthis) {
        nowthis.parent("#scroll_frame").bind("mouseenter", function() {
            nowthis.siblings("#scroll_bar").animate({
                opacity: 1
            }, 200);
        }).bind("mouseleave", function() {
            setTimeout(function() {
                nowthis.siblings("#scroll_bar").animate({
                    opacity: 0
                }, 200);
            }, 3000);
        });
    }
})(jQuery);